#include "action.hpp"
#include"sys_gpio.hpp"
#include"altera.hpp"
#include"jbiexprt.hpp"
#include"altera-jtag.hpp"

extern uint8_t* file_ptr;
extern uint32_t file_len;
bool ALTERA_Download(websocketpp::connection_hdl hdl,uint8_t *jbi_program,uint32_t length,int is_fpga);
void show(text_t t)
{
	std::cout << "ctl:" << t.ctl << std::endl
		<< "type:" << t.type << std::endl
		<< "total:" << t.length_total << std::endl
		<< "number:" << t.number << std::endl
		<< "len:" << t.length_data << std::endl;
}

int action_recvfile(websocketpp::connection_hdl hdl, text_t& text)
{
	static int count = 0;
	static uint8_t* ptr = nullptr;
	static int total = 0;
	if (text.ctl == 0x01)
	{
		if (text.type == 0x03)
		{
			std::cout << "begin..." << std::endl;
			if (file_ptr != nullptr)
				delete file_ptr;
			file_ptr = new uint8_t[text.length_total];
			total = text.length_total;
			ptr = file_ptr;
			count = 0;
			return 0;
		}
		else
		{
			std::cout << "err!" << std::endl;
			return -1;
		}
	}
	if (text.ctl == 0x02)
	{
		if (text.type == 0x03 && text.length_total == total)
		{
			if (text.number == count)
			{
				memcpy(ptr, text.data, text.length_data);
				std::cout << "data:" << std::endl;
				for (int i = 0; i < text.length_data; i++)
				{
					printf("%d ", text.data[i]);
					if (i % 128 == 0)printf("\n");
				}
				printf("\n");
				count++;
				ptr += text.length_data;

				if (ptr - file_ptr == text.length_total)
				{
					std::cout << "file:" << std::endl;
					for (int i = 0; i < text.length_total; i++)
					{
						printf("%d ", file_ptr[i]);
						if (i % 128 == 0)printf("\n");
					}
					printf("\n");
					file_len = text.length_total;
					return 0;
				}
				else
					return 1;
			}
		}
	}
	return -1;
}

int action_judgment(websocketpp::connection_hdl hdl,text_t& text)
{
	show(text);
	if (text.ctl == 0x01)
	{
		switch (text.type)
		{
		case 1:break;
		case 2:break;
		case 3:
			if (action_recvfile(hdl,text) == 0)
			{
				text_t text;
				
				text.ctl = 0x01;
				text.type = 0x05;
				text.length_total = 0x0;
				m_endpoint.socket_send(hdl,text);
				
			}
			break;
		case 4:
			if(action_flashfpga(hdl,text)==0)
			{
				text_t text;
				text.ctl = 0x01;
				text.type = 0x07;
				text.length_total = 0x000849;
				text.number = 0x0;
				m_endpoint.socket_send(hdl, text);
			}
			break;
		default:std::cout << "error" << std::endl; break;
		}
	}
	else
	{
		switch (text.type)
		{
		case 1:break;
		case 2:break;
		case 3:
			if (action_recvfile(hdl,text) == 0)
			{
				text_t text;
				text.ctl = 0x02;
				text.type = 0x05;
				text.length_total = 0x000849;
				text.number = 0x0;
				m_endpoint.socket_send(hdl, text);
			}
			break;
		default:std::cout << "error" << std::endl; break;
		}
	}
	return 0;
}

int action_flashfpga(websocketpp::connection_hdl hdl,text_t& text)
{
	if (text.ctl == 0x01 && text.type == 0x04)
	{
		if (file_ptr == nullptr)return -1;
		char temp[1472];
		char cmd[1472];
		memcpy(temp,text.data,text.length_data);
		sprintf(cmd,"sudo ffmpeg -i \"rtsp://192.168.1.10:554/user=admin&password=&channel=1&stream=0.sdp?real_stream\" -vcodec copy -acodec copy -f flv \"%s\" \n",temp);
		std::cout<<cmd<<std::endl;
		system(cmd);
		std::cout<<"over"<<std::endl;
		int lvalue = -1;
		sys_gpio_init();
		sys_gpio_start();
		printf("alter start\r\n");
		if (ALTERA_Download(hdl,file_ptr, file_len, 1))
		{
			printf("cpld ok!\r\n");
			lvalue = 0;
		}
		else 
			printf("cpld err!\r\n");

		if (file_ptr != NULL)
		{
			delete file_ptr;
			file_ptr = nullptr;
		}
		printf("alter stop\r\n");
		sys_gpio_stop();
		return lvalue;
	}
	else
	{
		return -1;
	}
}